package demoflink.window;

import demoflink.entity.WaterSensor;
import demoflink.function.WaterSensorMapFunction;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class WindowProcessApi {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        KafkaSource<String> kafkaSource = KafkaSource.<String>builder()
                .setBootstrapServers("node1:9092,node2:9092,node3:9092")
                .setGroupId("local")
                .setTopics("first")
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .setStartingOffsets(OffsetsInitializer.latest())
                .build();
        SingleOutputStreamOperator<WaterSensor> source = env.fromSource(kafkaSource, WatermarkStrategy.noWatermarks(), "kafkaSource").map(new WaterSensorMapFunction());

        KeyedStream<WaterSensor, String> keyedStream = source.keyBy(x -> x.getId());
        WindowedStream<WaterSensor, String, TimeWindow> window = keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(10)));
//        window.apply(new WindowFunction<WaterSensor, String, String, TimeWindow>() {
//            /**
//             *
//             * @param s 分组的key
//             * @param timeWindow 窗口对象
//             * @param iterable 保存的数据
//             * @param out 采集器
//             * @throws Exception
//             */
//            @Override
//            public void apply(String s, TimeWindow timeWindow, Iterable<WaterSensor> iterable, Collector<String> out) throws Exception {
//
//            }
//        });


        //输入类型  输出类型 key的类型 窗口的类型
        window.process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
            @Override
            public void process(String s, Context context, Iterable<WaterSensor> iterable, Collector<String> collector) throws Exception {

                long start = context.window().getStart();
                long end = context.window().getEnd();

                String windowStart = DateFormatUtils.format(start, "yyyy-MM-dd HH:mm:ss");
                String windowEnd = DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss");
                long count = iterable.spliterator().estimateSize();
                collector.collect("key="+s+"的窗口["+windowStart+","+windowEnd+"]包含 "+count+" 条数据=====>"+iterable.toString());

            }
        }).print();
        env.execute();
    }



}
